import java.util.Stack;

public class MaximalRectangle {

    public static void main(String[] args) {
        char[][] nums = {{'1'}};
        MaximalRectangle maximalRectangle = new MaximalRectangle();
        System.out.println(maximalRectangle.maximalRectangle(nums));
    }

    public int maximalRectangle(char[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] length = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == '1') {
                    length[i][j] = 1;
                    if (i != 0) length[i][j] += length[i - 1][j];
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < m; i++) {
            ans = Math.max(ans, maxArea(length[i]));
        }
        return ans;
    }

    public int maxArea(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
                Integer pop = stack.pop();
                ans = Math.max(ans, (i - (stack.isEmpty() ? -1 : stack.peek()) - 1) * nums[pop]);
            }
            stack.push(i);
        }
        while (!stack.isEmpty()) {
            Integer pop = stack.pop();
            ans = Math.max(ans, (nums.length - (stack.isEmpty() ? -1 : stack.peek()) - 1) * nums[pop]);
        }
        return ans;
    }
}
